// pages/index/index.ts
import { getWeather, backLogin } from "../../api/auth/user";
import { getListAll, indexInfo, Hrztoal, ChargeData, ChargeDataDay, yearsData, onlineDatas } from '../../api/site-monitoring'
import type { userInfo, siteList } from "../../type/type";
import { alarmList, PendingOrders, stopOrder, orderStatus } from '../../api/order'
import { FHInfo,vilageEcharts } from '../../api/echarts'
const dayjs = require('../../utils/day.js');
const day = dayjs();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLeft: false as Boolean,
    userInfo: {} as userInfo,
    tabsName: "设备报警" as String,
    alarmList: [] as siteList[],
    robberyList: [],
    orderList: [],
    weatherInfo: {},
    month: '' as String,
    day: '' as String,
    week: '' as String,
    echarts1List: [
    ] as String[],
    echarts2List: [
    ] as String[],
    echarts3List:[] as string[],
    show: false as Boolean,
    info: {} as object,
    heat: '' as string,
    flow: '' as string,
    total: {} as object,
    number: 0 as number,
    practical: '' as string,
    forecast: '' as string,
    isOnline: {} as object,
    status: "",
    collectNum: 0,
    installedNum: 0,
    onLineNum: 0,
    offLineNum: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {


    // this.getAlarmList();
    // this.getRobberyList();
    // this.PendingOrders();
    // this.setData({
    //     userInfo:wx.getStorageSync("userInfo").data,
    //     month:dayjs().month()+1,
    //     day:dayjs().date(),
    //     week:this.setDate(dayjs().day())

    // })
    // let city = wx.getStorageSync("userInfo").data.city
    // getWeather(city).then((res:any)=>{
    //     this.setData({
    //         weatherInfo:res.data
    //     })
    //     // console.log(res)
    // })
  },
  // onTapShow(){
  //   this.setData({
  //     show:true
  //   })
  //   console.log(this.data.show)
  // },
  //获取首页所有数据
  getIndexInfo() {
    const comID = wx.getStorageSync("userInfo").data.comID
    const userID = wx.getStorageSync("userInfo").data.userID;
    this.setData({
      echarts1List: [],
      echarts2List: []
    })
    wx.request({
      url: 'https://www.hbdinkon.cn:10020/MobileLinkCenter/equipment/selectEquipments',
      method: 'POST',
      data: {
        type: 0,
        stationType: -1,
        onState: '',
        searchString: '',
        comID: comID,
        userID: userID,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: (res) => {
        let count = res.data.data.filter(item => item.warnCode == 2)
        let length = count.length
        console.log(length)
        wx.setTabBarBadge({
          index: 1,
          text: String(length)
        })
      }
    })
    indexInfo(comID).then((res: any) => {
      console.log(res)
      this.setData({
        info: res.data,
        heat: res.data.供水累积热量,
        flow: res.data.供水累计流量
      })
    })
    Hrztoal(comID).then((res: any) => {
      this.setData({
        total: res.data
      })
    })
    ChargeDataDay(comID).then((res: any) => {
      this.setData({
        practical: res.data.日实际能耗,
        forecast: res.data.日预测能耗
      })
      // console.log(res)
    })
    yearsData(comID).then((res: any) => {
      this.setData({
        echarts1List: res.data
      })
      // console.log(res)
    })
    onlineDatas(comID).then((res: any) => {
      this.setData({
        isOnline: res.data
      })
      // console.log(res)
    })
    this.getEcharts();
  },
  isLogin() {
    wx.showLoading({
      title: '登录中...',
      mask: true
    })
    if (wx.getStorageSync("userInfo")) {
      wx.hideLoading()
    } else {
      wx.showModal({
        title: '登录失败',
        showCancel: false,
        confirmText: '确定',
        content: '用户登录信息失效,请重新登录',
        success: (res) => {
          if (res.confirm) {
            wx.reLaunch({
              url: '/pages/login/login'
            })
          }
        }
      })
      wx.hideLoading()
    }
  },
  async backLogin() {
    const comID = wx.getStorageSync("userInfo").data.comID
    const openID = wx.getStorageSync("userInfo").data.openID
    const res = await backLogin(openID, comID, this.data.userInfo.userID)
    if (res.data == 1) {
      wx.reLaunch({
        url: '/pages/login/login'
      })
      wx.clearStorage()
    } else {
      wx.showToast({
        title: '退出失败',
        icon: 'error'
      })
    }
  },
  handleMenu() {
    this.setData({
      isLeft: true,
    });
  },
  onClose() {
    this.setData({
      isLeft: false,
    });
  },
  onItem(e: any) {
    this.setData({
      tabsName: e.currentTarget.dataset.type,
    });
  },
  getAlarmList() {
    let comID = wx.getStorageSync("userInfo").data.comID
    let userID = wx.getStorageSync("userInfo").data.userID
    getListAll('1', '-1', '', comID, userID, '').then((res: any) => {
      this.setData({
        alarmList: res.data.data
      })
      // console.log(res)
    })
  },
  getRobberyList() {
    let comID = wx.getStorageSync("userInfo").data.comID
    alarmList(comID).then((res: any) => {
      this.setData({
        robberyList: res.data.data
      })
    })
  },
  PendingOrders() {
    let comID = wx.getStorageSync("userInfo").data.comID
    let userID = wx.getStorageSync("userInfo").data.userID
    PendingOrders(comID, userID).then((res: any) => {
      this.setData({
        orderList: res.data.data
      })
    })
  },
  setDate(week: number) {
    let numCn = ['一', '二', '三', '四', '五', '六', '日'] as String[];
    if (week >= 1 && week <= 7) {
      return numCn[week - 1]
    }
  },
  getEcharts() {
    const comID = wx.getStorageSync("userInfo").data.comID
    const number = this.data.number
    ChargeData(comID, number).then((res: any) => {
      this.setData({
        echarts2List: res.data.data
      })
      // console.log(res)
    })
  },
  lastDay() {
    this.setData({
      number: --this.data.number
    })
    this.getEcharts()
  },
  nextDay() {
    this.setData({
      number: ++this.data.number
    })
    this.getEcharts()
  },
  getstatus() {
    const that = this;
    const comID = wx.getStorageSync("userInfo").data.comID;
    const userID = wx.getStorageSync("userInfo").data.userID;
    orderStatus(comID, userID).then((res: any) => {
      that.setData({
        status: res.data
      })
    })
  },
  onStatus() {
    const that = this;
    const status = that.data.status;
    const comID = wx.getStorageSync("userInfo").data.comID;
    const userID = wx.getStorageSync("userInfo").data.userID;
    if (status == 0) {
      stopOrder(comID, userID, 1).then((res: any) => {
        if (res.data.code == 200) {
          this.getstatus()
        } else {
          wx.showToast({ title: "操作失败", icon: "error" });
        }
      })
    } else {
      stopOrder(comID, userID, 0).then((res: any) => {
        if (res.data.code == 200) {
          this.getstatus()
        } else {
          wx.showToast({ title: "操作失败", icon: "error" });
        }
      })
    }
  },
  getVillage() {
    const comID = wx.getStorageSync("userInfo").data.comID;
    FHInfo(comID).then((res:any)=>{
      this.setData({
        collectNum: res.data.collectNum,
        installedNum: res.data.installedNum,
        onLineNum: res.data.onLineNum,
        offLineNum: res.data.offLineNum,
      });
    });
  },
  getData () {
		const comID = wx.getStorageSync("userInfo").data.comID;
		vilageEcharts(comID).then((t:any)=> {
			t.data && this.setData({ echarts3List: t.data });
		});
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.isLogin();
    this.getAlarmList();
    this.getRobberyList();
    this.PendingOrders();
    this.getIndexInfo();
    this.getstatus();
    this.getData();
    this.getVillage();
    this.setData({
      userInfo: wx.getStorageSync("userInfo").data,
      month: dayjs().month() + 1,
      day: dayjs().date(),
      week: this.setDate(dayjs().day())

    })
    let city = wx.getStorageSync("userInfo").data.city
    const comID = wx.getStorageSync("userInfo").data.comID
    getWeather(comID, city).then((res: any) => {
      this.setData({
        weatherInfo: res.data
      })
      // console.log(res)
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
